<script>
  var maxAreaOfIsland = function(grid) {
    if (grid.length === 0) {
      return 0;
    }
    let max = 0;
    for(let row = 0; row < grid.length; row++) {
      for(let col = 0; col < grid[0].length; col++) {
        if (grid[row][col] === 1) {
          let count = dfs(row, col);
          max = Math.max(max, count);
        }
      }
    }

    function dfs(row, col) {
      if (row < 0 || row >= grid.length || col < 0 || col >= grid[0].length || grid[row][col] !== 1){
        return 0;
      }
      // 因为外面判断了当前入口是1才会进来执行dfs，所以初始数量就为1
      let count = 1;
      // 沉没当前地块（单元格）
      grid[row][col] = 0;

      // 往上下左右查找
      count += dfs(row + 1, col);
      count += dfs(row - 1, col);
      count += dfs(row, col + 1);
      count += dfs(row, col - 1);

      return count;
    }

    return max;
  };

  console.log(maxAreaOfIsland([[0,0,1,0,0,0,0,1,0,0,0,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,1,1,0,1,0,0,0,0,0,0,0,0],[0,1,0,0,1,1,0,0,1,0,1,0,0],[0,1,0,0,1,1,0,0,1,1,1,0,0],[0,0,0,0,0,0,0,0,0,0,1,0,0],[0,0,0,0,0,0,0,1,1,1,0,0,0],[0,0,0,0,0,0,0,1,1,0,0,0,0]]))
  console.log(maxAreaOfIsland([[0,0,0,0,0,0,0,0]]))
</script>